<?php

namespace app\logic\im;

use app\logic\im\BaseLogic;
use app\common\model\User;
// use app\admin\model\im\Account AS ImAccount;

// use app\common\enum\fp\PaymentType;
// use app\common\model\fp\PostAudit;
// use app\common\model\fp\PostIndex;
// use app\common\model\fp\PostMedia;

// use think\facade\Event;
// use think\facade\Log;

class SnsLogic extends BaseLogic
{
    // 拉取好友
    public function friendList(int|string $user_id, int $start_index = 0, int $standard_sequence = 0) : array
    {
        // 判断 $user_id 是否为数字
        $from_account = is_numeric($user_id) 
        ? $this->im_config['prefix'] . $user_id 
        : $user_id;

        $post_data = [
            'From_Account' => $from_account, // 指定要拉取好友数据的用户的 UserID
            'StartIndex' => $start_index, // 分页的起始位置
            'StandardSequence' => $standard_sequence // 上次拉好友数据时返回的 StandardSequence，如果 StandardSequence 字段的值与后台一致，后台不会返回标配好友数据
        ];

        $path = 'v4/sns/friend_get';

        return $this->postraw($path, $post_data);
    }

    // 同步好友列表
    public function syncFriendList(array $data) : bool
    {
        $from_account = isset($data['from_account']) ? $data['from_account'] : '';

        $from_account_arr = [];
        if(!$from_account){
            $user_id_arr = User::initModel()->field('id')->select()->toArray();
            if($user_id_arr && count($user_id_arr) > 0){
                foreach ($user_id_arr as  $value) {
                    $from_account_arr[] = $this->im_config['prefix'] . $value['id'];
                }
            }
        }else{
            $from_account_arr[] = $from_account;
        }

        if(empty($from_account_arr)){
            return false;
        }

        $call_limit = 200; // 每分钟调用限制
        $time_window = 60; // 时间窗口（秒）
        $call_count = 0;   // 当前调用计数
        $start_time = time(); // 时间窗口起点

        foreach ($from_account_arr as $value) {
            // 检查是否超过调用限制
            if ($call_count >= $call_limit) {
                $elapsed_time = time() - $start_time;
                if ($elapsed_time < $time_window) {
                    // 等待直到时间窗口结束
                    sleep($time_window - $elapsed_time);
                }
                // 重置计数器和时间窗口
                $call_count = 0;
                $start_time = time();
            }

            // 调用 friendList 方法
            $res = $this->friendList($value);
            $call_count++; // 增加调用计数

            if (isset($res['ErrorCode']) && $res['ErrorCode'] == 0 && isset($res['UserDataItem']) && count($res['UserDataItem']) > 0) {
                foreach ($res['UserDataItem'] as $key => $val) {
                    // 处理返回数据
                    print_r($val); //this todo🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉
                    //this todo🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉
                    //this todo🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉
                    //this todo🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉
                    //this todo🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉🍉
                }
            } else {
                custom_log(json_encode($res, 256), null, 'sync_friend_list_error'); // 记录错误日志
                return false;
            }
        }

        return true;
    }
}